import Vue from 'vue'
import moment from 'moment'
export function trim(str) {
  return str.replace(/(^\s*)(\s*$)/g, '')
}
export function timeformat(str, pattern = 'YYYY-MM-DD HH:mm:SS') {
  return str && moment(str).format(pattern)
}
export function dateformat(str, pattern = 'YYYY-MM-DD') {
  return str && moment(str).format(pattern)
}
export function phpDateFormat(time, pattern = 'YYYY-MM-DD') {
  return time && moment(time * 1000).format(pattern)
}
export function phpTimeFormat(time, pattern = 'YYYY-MM-DD HH:mm:SS') {
  return time && moment(time * 1000).format(pattern)
}

export function renderSize(value) {
  if (value == null || value === '') {
    return '0 Bytes'
  }
  const unitArr = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
  let index = 0
  const srcsize = parseFloat(value)
  index = Math.floor(Math.log(srcsize) / Math.log(1024))
  let size = srcsize / Math.pow(1024, index)
  size = size.toFixed(2) // 保留的小数位数
  return size + unitArr[index]
}
export function notEmptyQuery(a) {
  if (!a || a === {}) return false
  for (const i in a) if (i !== 'orderby' && a[i]) return true
  return false
}
const filters = {
  trim,
  timeformat,
  dateformat,
  renderSize,
  phpDateFormat,
  phpTimeFormat,
  notEmptyQuery
}
export default filters
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
})
